#include "./salve_conn.h"
#include "../driver/cmd_uart.h"
#include "../driver/can_bus.h"
#include "../conn/proto.h"
#include "../task/query_task.h"
#include "../task/handler_task.h"

namespace service {

using namespace utils;
using namespace conn;
using namespace driver;

const ConnInfo STATUS_CONN_INFO(proto::REQ_UPLOAD_STATUS, 0, ConnInfo::TYPE_NOTIFY, MY_ADDR, addr::IPC);

void salve_dispatch(const Message &msg) {
    using namespace task;

    switch (msg.conn_info.req) {

        case proto::REQ_GET_TASK_INFO:
        case proto::REQ_PING:
            queryTask->post(msg);
        break;

#if defined (BOARD_HEAT)
        case proto::REQ_HEAT_GET_LOW:
            queryTask->post(msg);
        break;
#endif

        default:
            handlerTask->post(msg);
		break;
    }
}

Error SalveConn::write_resp_wait_ack(ConnType conn_type, const ConnInfo &conn_info, uint8_t len) {
    switch (conn_type) {
        case ConnType::RS232:
            return cmdUart->write_data_wait_ack(conn_info, m_resp_buf, len);

        case ConnType::CAN:
            return canBus->write_ext_pkg_wait_ack(conn_info, m_resp_buf, len);

        default:
            return ec::INVALID;
    }
}

void SalveConn::write_status_if_changed(uint8_t len) {
    if (memcmp(m_status_buf, m_status_tmp_buf, len) == 0) {
        return;
    }
    memcpy(m_status_tmp_buf, m_status_buf, len);
    canBus->write_ext_pkg(STATUS_CONN_INFO, m_status_buf, len);
    cmdUart->write_data(STATUS_CONN_INFO, m_status_buf, len);
}



}
